package xyz.icxl.kotlion.ext.binding

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.databinding.BindingAdapter
import androidx.databinding.BindingConversion
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils


/**
 * 绑定Text
 * @param textResId 字符串资源Id
 * @param appendTextResId 追加的字符串资源Id
 * @param appendTextColorResId 追加的字符串颜色资源Id
 */
@BindingAdapter(
    "ext_contentText",
    "ext_appendText",
    "ext_appendTextColorResId",
    requireAll = true
)
fun extBindText(
    textView: TextView,
    contentText: CharSequence,
    appendText: CharSequence,
    @ColorInt appendTextColorResId:Int
) {
    textView.text = SpannableStringBuilder().apply {
        append(contentText)
        append(
            appendText,
            ForegroundColorSpan(appendTextColorResId),
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
    }
}
@BindingAdapter(
    "ext_contentStringId",
    "ext_appendStringId",
    "ext_appendTextColorResId",
    requireAll = true
)
fun extBindText2(
    textView: TextView,
    @StringRes contentStringId: Int,
    @StringRes appendStringId: Int,
    @ColorInt appendTextColorResId:Int
) {
    textView.text = SpannableStringBuilder().apply {
        append(StringUtils.getString(contentStringId))
        append(
            StringUtils.getString(appendStringId),
            ForegroundColorSpan(appendTextColorResId),
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
    }
}